package com.funsport.controller.mobile;

import com.funsport.common.Result;
import com.funsport.service.WeatherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 移动端天气控制器
 */
@Tag(name = "移动端天气管理", description = "天气信息相关接口")
@RestController
@RequestMapping("/mobile/weather")
@RequiredArgsConstructor
@Slf4j
public class MobileWeatherController {

    private final WeatherService weatherService;

    /**
     * 获取实时天气
     */
    @Operation(summary = "获取实时天气", description = "根据城市名称获取当前天气信息")
    @GetMapping
    public Result<Map<String, Object>> getWeather(
            @Parameter(description = "城市名称") @RequestParam(required = false, defaultValue = "北京") String city) {
        
        log.info("获取天气信息 - 城市: {}", city);
        
        try {
            Map<String, Object> weatherData = weatherService.getWeather(city);
            log.info("天气信息获取成功: {}", weatherData);
            return Result.success(weatherData);
        } catch (Exception e) {
            log.error("获取天气信息失败: {}", e.getMessage(), e);
            return Result.error("获取天气信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取运动建议
     */
    @Operation(summary = "获取运动建议", description = "根据天气情况获取运动建议")
    @GetMapping("/suggestion")
    public Result<Map<String, Object>> getSportSuggestion(
            @Parameter(description = "城市名称") @RequestParam(required = false, defaultValue = "北京") String city,
            @Parameter(description = "运动类型") @RequestParam(required = false, defaultValue = "篮球") String sportType) {
        
        log.info("获取运动建议 - 城市: {}, 运动类型: {}", city, sportType);
        
        try {
            Map<String, Object> suggestion = weatherService.getSportSuggestion(city, sportType);
            return Result.success(suggestion);
        } catch (Exception e) {
            log.error("获取运动建议失败: {}", e.getMessage(), e);
            return Result.error("获取运动建议失败: " + e.getMessage());
        }
    }

    /**
     * 获取天气预报
     */
    @Operation(summary = "获取天气预报", description = "获取未来几天的天气预报")
    @GetMapping("/forecast")
    public Result<Map<String, Object>> getWeatherForecast(
            @Parameter(description = "城市名称") @RequestParam(required = false, defaultValue = "北京") String city,
            @Parameter(description = "预报天数") @RequestParam(required = false, defaultValue = "3") Integer days) {
        
        log.info("获取天气预报 - 城市: {}, 天数: {}", city, days);
        
        try {
            Map<String, Object> forecast = weatherService.getWeatherForecast(city, days);
            return Result.success(forecast);
        } catch (Exception e) {
            log.error("获取天气预报失败: {}", e.getMessage(), e);
            return Result.error("获取天气预报失败: " + e.getMessage());
        }
    }
}